package com.bt.mes.common.util;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonUtil {

	/**
	 * 
	 * /** 将一个 Object 或者List对象转化为JSONObject或者JSONArray
	 * 
	 * @param ObjOrList
	 *            Object 或者List对象
	 * @return
	 */
	private static ObjectMapper mapper = new ObjectMapper();;
	private static final Logger logger = Logger.getLogger(JacksonUtil.class);

	/**
	 * 将JSON字符串转换成指定的对象
	 * 
	 * usage: SampleIn sampleIn =
	 * JacksonUtil.fromJson(jsonString,SampleIn.class);
	 * 
	 * @param jsonString
	 * @param clazz
	 * @return object
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T fromJson(String jsonString, @SuppressWarnings("rawtypes") Class clazz)
			throws JsonParseException, JsonMappingException, IOException {

		if (jsonString == null) {
			return null;
		}
		return (T) mapper.readValue(jsonString, clazz);
	}

	/**
	 * 将对象转换成JSON字符串
	 * 
	 * usage : String outMsg = JacksonUtil.toJsonString(SampleOut);
	 * 
	 * @param object
	 * @return JSON字符串
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonGenerationException
	 * @throws Exception
	 */
	public static String toJsonString(Object object) {
		String jsonString = null;
		try {
			jsonString = mapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			logger.error(e.toString());
		}
		return jsonString;
	}

	/**
	 * 从JSON字符串中读取指定结点的内容
	 * 
	 * usage: String name = (String)JacksonUtil.getValue(jsonString,name);
	 * 
	 * @param jsonString
	 * @param key
	 * @return object：结点内容
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public static Object getValue(String jsonString, String key)
			throws JsonParseException, JsonMappingException, IOException {
		@SuppressWarnings("unchecked")
		Map<String, Map<String, Object>> jsonMap = mapper.readValue(jsonString, Map.class);
		return jsonMap.get(key);
	}

	public static JSONObject getObjectValue(String key, JSONObject oj) {
		JSONObject b = null;
		try {
			b = oj.getJSONObject(key);
		} catch (Exception e) {
			e.printStackTrace();
			b = null;
		}
		return b;
	}

	public static boolean getBooleanValue(String key, JSONObject oj) {
		boolean b = false;
		try {
			b = oj.getBoolean(key);
		} catch (Exception e) {
			// e.printStackTrace();
			b = false;
		}
		return b;
	}

	public static int getIntValue(String key, JSONObject oj) {
		int str = 0;
		try {
			str = oj.getIntValue(key);
		} catch (Exception e) {
			str = 0;
		}
		return str;
	}

	public static String getStringValue(String key, JSONObject oj) {
		String str = "";
		try {
			str = oj.getString(key);
		} catch (Exception e) {
			str = "";
		}
		return str;
	}

	public static JSONArray getListValue(String key, JSONObject oj) {
		JSONArray str = null;
		try {
			str = oj.getJSONArray(key);
		} catch (Exception e) {
			str = null;
		}
		return str;
	}

	public static Object toJSON(Object ObjOrList) {
		Object obj = null;

		try {
			obj = JSON.toJSON(ObjOrList);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return obj;
	}

	/**
	 * 将一个 Object 或者List对象转化为JSOObject或者JSONArray
	 * 
	 * @param ObjOrList
	 *            Object 或者List对象 或者hashmap 但是如果是set 就会有问题
	 * @return
	 */
	public static String toJSONStr(Object ObjOrList) {
		String jsonstr = "";

		try {
			jsonstr = JSON.toJSONString(ObjOrList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return jsonstr;
	}

	// 第二部分字符串转 obj list

	/**
	 * 字符串转obj
	 * 
	 * @param jsonstr
	 * @param clazz
	 * @return
	 */
	public static Object parseToObject(String jsonstr, Class<?> clazz) {
		Object parseObj = null;
		try {
			parseObj = JSON.parseObject(jsonstr, clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return parseObj;
	}

	/**
	 * 字符串转list
	 * 
	 * @param jsonstr
	 * @param clazz
	 * @return
	 */
	public static List<?> parseToList(String jsonstr, Class<?> clazz) {
		List<?> parseObj = null;
		try {
			parseObj = JSON.parseArray(jsonstr, clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return parseObj;
	}

	// 第三部分 字符串转JSONObj 或者JSONArray

	/**
	 * 字符串转jsonobj
	 * 
	 * @param jsonstr
	 * @return
	 */
	public static JSONObject parseToJSONObejct(String jsonstr) {
		JSONObject parseObj = null;
		try {
			parseObj = JSON.parseObject(jsonstr);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return parseObj;
	}

	/**
	 * 字符串转list
	 * 
	 * @param jsonstr
	 * @return
	 */
	public static JSONArray parseToJSONArray(String jsonstr) {
		JSONArray parseObj = null;
		try {
			parseObj = JSON.parseArray(jsonstr);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return parseObj;
	}

	// 第四部分 com.alibaba包下 JSONObj 或者JSONArr 转 javabean或者 java array
	/**
	 *
	 * @param jsonObj
	 * @param obj
	 * @return
	 */
	public static Object parseToObject(JSONObject jsonObj, Class<?> obj) {
		Object parseObj = null;
		try {
			parseObj = JSON.parseObject(jsonObj + "", obj);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return parseObj;
	}

	/**
	 *
	 * @param jsonArr
	 * @param obj
	 * @return
	 */
	public static List<?> parseToList(JSONArray jsonArr, Class<?> obj) {
		List list = null;

		try {
			list = JSON.parseArray(jsonArr + "", obj);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return list;
	}
}
